﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace ZombieMonkeys.Vibria.Logic
{
    /// <summary>
    /// The main component of the sudoku puzzle
    /// </summary>
    public class Square
    {
        //how do we really want note to work?
        string note;
        const int maxNotes = 4;

        /// <summary>
        /// initialized in the contructor to be no greater than size of 4, to avoid IndexOutOfBoundsExceptions
        /// </summary>
        int[] noteValue;// = new int[4];
        /// <summary>
        /// Initialized as an int[4] and that is it's max size
        /// </summary>
        public int[] NoteValue
        {
            get { return noteValue; }
            set { noteValue = value; }
        }

        int value; //this could also be a char, instead of int

        public int Value
        {
            get { return value; }
            
        }
       // MiniGrid miniGrid = new MiniGrid(row, col);

        //public MiniGrid MiniGrid
        //{
        //    get { return miniGrid; }
            
        //}

        //this will be a string representation of the squares value
        string text;

        //bool answer for future use in ink mode
        bool permanent;

        bool penMode;

        int row;

        int col;

        int miniGrid;

        Point point;

        public Point Point
        {
            get 
            {
                //point = new Point(row, col);
                return point; 
            }
            set { point = new Point(row, col); }
        }
 
        /// <summary>
        /// Default constructor, initializes the noteValues array to be of const int size 4
        /// </summary>
        public Square()
        {
            
            value = 0;
            noteValue = new int[maxNotes];

            //initialize all values of the array to be 0
            clearNotes();
        }

        /// <summary>
        /// Method to change to stored value of a square
        /// </summary>
        /// <param name="newValue">the value to assign</param>
        public void SetValue(int ? newValue)
        {
            //made a few changes here so that i can set the squares to string .empty if i pass a 0 value to them
            string newText = String.Empty;

            //get the text value from the value passed in
            value = 0;

            if (newValue.HasValue)
            {
                value = newValue.Value;
                //set to string.empty
                newText = value.ToString();
            }
            setText(newText);
        }
        /// <summary>
        /// Set the Point to the row and col of the square
        /// </summary>
        public void SetPoint()
        {
            point = new Point(row, col);
        }

        /// <summary>
        /// Gets a string of the value for the square
        /// </summary>
        /// <returns>a string representation of the value for a square</returns>
        public string GetValue()
        {
            return value.ToString();
        }
        /// <summary>
        /// Get the string of a value by index in the NoteValues array
        /// </summary>
        /// <param name="index">the index you want to see</param>
        /// <returns>the value as a string</returns>
        public string GetNoteValue(int index)
        {
            return noteValue[index].ToString();
        }

        //if we are going to have this we need to decide this soon so we can design it in or out
        //this is if we want to design our sudoku to handle more than just numbers
        public void setPicture(string picFile)
        {
            setPic(picFile);
        }

        /// <summary>
        /// the ability to set a picture for a sudoku square
        /// </summary>
        /// <param name="pic">the file that represents the graphic</param>
        private void setPic(string pic)
        { 
        
        }

        private void setText(string numberText)
        {
            text = numberText;
        }

        /// <summary>
        /// Set the row that a square belongs to
        /// </summary>
        /// <param name="gridRow">the squares row</param>
        public void SetRow(int gridRow)
        {
            
            row = gridRow;
           // miniGrid.Row = row;
        }

        /// <summary>
        /// Gets the row for the UI to use
        /// </summary>
        /// <returns>row number</returns>
        public int GetRow()
        {
            return row;
        }

        /// <summary>
        /// Set the Column that a square belongs to
        /// </summary>
        /// <param name="gridColumn">the squares column</param>
        public void SetColumn(int gridColumn)
        {
            col = gridColumn;
           // miniGrid.Col = col;
        }

        /// <summary>
        /// Gets the column number for the UI
        /// </summary>
        /// <returns>column number</returns>
        public int GetColumn()
        {
            return col;
        }

        /// <summary>
        /// Set the minigrid that a square belongs to
        /// </summary>
        /// <param name="g"></param>
        public void SetGrid(int g)
        {
            miniGrid = g;
        }

        public void SetPermanent(bool permanency)
        {
            permanent = permanency;
        }

        /// <summary>
        /// Gets the grid number for the UI
        /// </summary>
        /// <returns>minigrid number</returns>
        public int GetGrid()
        {
            return miniGrid;
        }

        /// <summary>
        /// Get whether this square is permanent for the Grid, as part of the puzzle
        /// </summary>
        /// <returns>a bool of the permanence of the square</returns>
        public bool GetPermanency()
        {
            return permanent;
        }

        /// <summary>
        /// Use this method to assign a value to the appropriate index of the array
        /// </summary>
        /// <param name="index">the chosen index of the array</param>
        /// <param name="value">the value to assign to that index</param>
        public void SetNote(int index, int value)
        {
            //could put a try catch in here, but i personally think we should handle it better
            //because we know the potential for someone to pass an invalid index to an array
            noteValue[index] = value;

        }

        /// <summary>
        /// Calling this method will cause the Square to reset all of it's notes to zero again
        /// </summary>
        private void clearNotes()
        {
            for (int i = 0; i < noteValue.Count(); i++)
            {
                noteValue[i] = 0;
            }
        }
        
        //for now we can just set the note function as a string primarily for use by the solver
        //When we have time, we can add the functionality to use it in the GUI
        public string Note
        {
            get
            {
                return note;
            }
            set
            {
                note = value;
            }
        }

        /// <summary>
        /// Clears out the value in the current square
        /// </summary>
        public void Clear()
        {
            text = "";
        }
    }
}
